#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

struct Trienode {
    bool isEnd;
    vector<Trienode*> next;
    Trienode() : isEnd(false), next(26, nullptr) {}
};

class Trie {
private:
    Trienode* root;
public:
    Trie() {
        root = new Trienode();
    }
    void insert(string word) {
        Trienode* node = root;
        for (char c : word) {
            int index = c - 'a';
            if (!node->next[index]) {
                node->next[index] = new Trienode();
            }
            node = node->next[index];
        }
        node->isEnd = true;
    }
    bool find(string word) {
        Trienode* node = root;
        for (char c : word) {
            int index = c - 'a';
            if (!node->next[index]) {
                return false;
            }
            node = node->next[index];
        }
        return node->isEnd;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, l;
    cin >> n;
    vector<Trie> trees(n);
    cin.ignore();
    for (int i = 0; i < n; i++) {
        string line;
        getline(cin, line);
        istringstream ss(line);
        ss >> l;
        string word;
        while (ss >> word) {
            trees[i].insert(word);
        }
    }

    cin >> m;
    cin.ignore();
    for (int i = 0; i < m; i++) {
        string word;
        cin >> word;
        bool found = false;
        for (int j = 0; j < n; j++) {
            if (trees[j].find(word)) {
                if (found) {
                    cout << " ";
                }
                cout << j + 1;
                found = true;
            }
        }
        if (!found) {
            cout << "";
        }
        cout << endl;
    }
    return 0;
}
